package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.OriginalRoleMember;
import com.sg.service.base.framework.MOriginalRoleMemberService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.OriginalRoleMemberMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOriginalRoleMemberServiceImpl
        extends ServiceImpl<OriginalRoleMemberMapper, OriginalRoleMember>
        implements MOriginalRoleMemberService {

    @Autowired
    OriginalRoleMemberMapper originalRoleMemberMapper;

    /**
     * 1-2-09批量查询员工身份人员列表[1259]
     * gen by moon at 10/16/2022, 11:06:47 AM
     */
    @Trace(operationName = "1-2-09批量查询员工身份人员列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OriginalRoleMember> batchQueryStaffBeginMemberList(BatchQueryStaffBeginMemberListReq reqDto) {

        try {
            QueryWrapper<OriginalRoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getStaffBeginMemberList() == null || reqDto.getStaffBeginMemberList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getStaffBeginMemberList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OriginalRoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OriginalRoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-09查询身份人员详情[174]
     * gen by moon at 10/16/2022, 11:06:46 AM
     */
    @Trace(operationName = "1-2-09查询身份人员详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OriginalRoleMember queryBeginMemberDetail(QueryBeginMemberDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OriginalRoleMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OriginalRoleMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OriginalRoleMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-09查询身份人员列表[80]
     * gen by moon at 10/16/2022, 11:06:45 AM
     */
    @Trace(operationName = "1-2-09查询身份人员列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OriginalRoleMember> queryBeginMemberList(QueryBeginMemberListReq reqDto) {

        try {
            QueryWrapper<OriginalRoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OriginalRoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("first_letter");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OriginalRoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-09新增身份人员[77]
     * gen by moon at 10/16/2022, 11:06:44 AM
     */
    @Trace(operationName = "1-2-09新增身份人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBeginMember(OriginalRoleMember originalRoleMember) {
        originalRoleMember.setOriginalRoleMemberId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        originalRoleMember.setIsTransaction("FALSE");
        originalRoleMember.setIsArchive("FALSE");
        originalRoleMember.setIsInvalid("FALSE");
        originalRoleMember.setIsValid("FALSE");
        originalRoleMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        originalRoleMember.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            originalRoleMember.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        originalRoleMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        originalRoleMember.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            originalRoleMember.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (super.save(originalRoleMember)) {
            return originalRoleMember.getOriginalRoleMemberId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-09批量查询身份人员列表	[1181]
     * gen by moon at 10/16/2022, 11:06:47 AM
     */
    @Trace(operationName = "1-2-09批量查询身份人员列表	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OriginalRoleMember> batchQueryBeginMemberList(BatchQueryBeginMemberListReq reqDto) {

        try {
            QueryWrapper<OriginalRoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("original_role_member_id", reqDto.getBeginMemberList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OriginalRoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OriginalRoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-09修改身份人员[78]
     * gen by moon at 10/16/2022, 11:06:45 AM
     */
    @Trace(operationName = "1-2-09修改身份人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBeginMember(OriginalRoleMember originalRoleMember) {
        originalRoleMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            originalRoleMember.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("ARCHIVING")) {
            originalRoleMember.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        originalRoleMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            originalRoleMember.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (originalRoleMember != null && originalRoleMember.getSubjectLifeCycle() != null && originalRoleMember.getSubjectLifeCycle().equals("ARCHIVING")) {
            originalRoleMember.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(originalRoleMember);
    }

    /**
     * 1-2-09批量修改身份人员[1617]
     * gen by moon at 10/16/2022, 11:06:49 AM
     */
    @Trace(operationName = "1-2-09批量修改身份人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateBeginMember(List<OriginalRoleMember> originalRoleMember) {

        if (originalRoleMember == null || originalRoleMember.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(originalRoleMember);

    }

    /**
     * 1-2-09批量删除身份人员[1615]
     * gen by moon at 10/16/2022, 11:06:48 AM
     */
    @Trace(operationName = "1-2-09批量删除身份人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteBeginMember(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M1批量查询身份人员测试[4861]
     * gen by moon at 10/16/2022, 11:06:49 AM
     */
    @Trace(operationName = "M1批量查询身份人员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OriginalRoleMember> batchQueryOriginalRoleMemberTestData(BatchQueryOriginalRoleMemberTestDataReq reqDto) {

        try {
            QueryWrapper<OriginalRoleMember> queryWrapper = new QueryWrapper<>();

            if (reqDto.getOriginalRoleMemberTestDataList() == null || reqDto.getOriginalRoleMemberTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getOriginalRoleMemberTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OriginalRoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OriginalRoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OriginalRoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删身份人员测试[4862]
     * gen by moon at 10/16/2022, 11:06:50 AM
     */
    @Trace(operationName = "M1批量删身份人员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteOriginalRoleMemberTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
